package cn.itcast.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * @program: springbootdemo
 * @description: 此数据源配置
 * @author: wxk
 * @create: 2020-12-21 09:55
 **/


@Configuration
@MapperScan(basePackages = "cn.itcast.dao.second",sqlSessionFactoryRef = "SqlSessionFactory")
public class SecondDataSourceConfig {

    @Bean(name = "DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.second")
    public DataSource StageConfig(){
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "SqlSessionFactory")
    public SqlSessionFactory oldSqlSessionFactory(@Qualifier("DataSource")DataSource dataSource) throws  Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/data2/*"));
        return bean.getObject();
    }

//    @Bean(name = "Transactionmanager")
//    public DataSourceTransactionManager oldTransactionManager(@Qualifier("DataSource") DataSource dataSource) {
//        return new DataSourceTransactionManager(dataSource);
//    }

//    @Bean(name = "SqlSessionTemplate")
//    public SqlSessionTemplate oldSqlSessionTemplate(@Qualifier("SqlSessionFactory")SqlSessionFactory sqlSessionFactory) {
//        return new SqlSessionTemplate(sqlSessionFactory);
//    }


}
